#认证蓝图
from flask import Blueprint,render_template, redirect, url_for, session, jsonify
from flask import Flask, request
from models import User, Product
from extends import db
from .forms import LoginForm

bp=Blueprint("auth",__name__,url_prefix="/auth")


#注册     暂不做该功能
@bp.route("/register")
def register():
    #表单验证:flask-wtf:wtforms
    return render_template("register.html")


#登录
@bp.route("/login",methods=['POST','GET'])
def login():
    if request.method == 'GET':
        return render_template('login.html')
        # # 判断是否已经在登录状态上:判断session中是否有uname的值
        # if 'username' in session:
        #     # 已经登录了，直接去往首页
        #     return redirect('/')
        # else:
        #     # 没有登录，继续向下判断cookie
        #     if 'username' in request.cookies:
        #         # 曾经记住过密码,取出值保存进session
        #         username = request.cookies.get('username')
        #         session['username'] = username
        #         return redirect('/')
        #     else:
        #         # 　之前没有登录过,去往登录页
        #         return render_template('login.html')
    else:
        # 先处理登录,登录成功继续则保存进session,否则回到登录页
        username = request.form.get('username')
        password = request.form.get('password')
        user = User.query.filter_by(username=username, password=password).first()
        if user:
            session['username'] = username
            if user.userType==1: #顾客首页
                return redirect('/')
            else: #管理员界面
                return redirect("/auth/admin")
        else:
            # 登录失败
            return redirect('/auth/login')

    # if request.method=='GET':
    #     print("GET登陆页面")
    #     return render_template("login.html")
    # else:
    #     proto = request.get_json()
    #     user = User.query.filter_by(username=proto["username"], password=proto["password"]).first()
    #     if user:
    #         return redirect(url_for("/"))
    #     else:
    #         return redirect(url_for("auth.login"))
    #     # form = LoginForm(request.form)
    #     # if form.validate():
    #     #     username = form.username.data
    #     #     password = form.password.data
    #     #     user = User.query.filter_by(username=username).first()
    #     #     if not user: #用户名不存在
    #     #         print("用户名在数据库中不存在")
    #     #         return redirect(url_for("auth.login"))
    #     #     if user.password==password: #密码输入正确
    #     #         print("密码正确，即将跳转到首页")
    #     #         session['user_id'] - user.uid
    #     #         return redirect("/") #跳转到首页
    #     # else:
    #     #     print(form.errors)
    #     #     return redirect(url_for("auth.login"))

#登出
@bp.route("/logout")
def logout():
    session.clear()
    return redirect("/")

#忘记密码
@bp.route("/forgot")
def forgot():
    return render_template("forgot_password.html")

#管理员首页
@bp.route("/admin")
def admin():
    return render_template("admin.html")

#管理员界面-客户信息维护页面
@bp.route("/customer_list")
def customer_list():
    customer_list = User.query.filter_by(userType=1)
    return render_template('customer_list.html', customer_list=customer_list)

#管理员界面-商品信息维护页面
@bp.route("/product_list")
def product_list():
    product_list =Product.query.all()
    return render_template('product_list.html', product_list=product_list)

@bp.route("/delete_product/<pid>")
def delete_product(pid):
    print( "后端删除pid="+pid+"的商品")
    product = Product.query.get(pid)
    if product:
        db.session.delete(product)
        db.session.commit()
        return redirect("/auth/product_list")
    return None

@bp.route("/delete_customer/<uid>")
def delete_customer(uid):
    print( "后端删除uid="+uid+"的用户")
    user = User.query.get(uid)
    if user:
        db.session.delete(user)
        db.session.commit()
        return redirect("/auth/customer_list")
    return None